package com.yunkazaixian.syntelecom.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yunkazaixian.syntelecom.entity.HttpResponse;
import com.yunkazaixian.syntelecom.entity.McSimDx;
import com.yunkazaixian.syntelecom.entity.McSimFlow;
import com.yunkazaixian.syntelecom.entity.Push;
import com.yunkazaixian.syntelecom.mapper.McSimDxMapper;
import com.yunkazaixian.syntelecom.mapper.McSimFlowMapper;
import com.yunkazaixian.syntelecom.service.DXService;
import com.yunkazaixian.syntelecom.service.IMcSimFlowService;
import com.yunkazaixian.syntelecom.utils.DateUtils;
import com.yunkazaixian.syntelecom.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 流量同步Controller
 * </p>
 *
 * @author Lin
 * @since 2024-03-21
 */
@Controller
@RequestMapping("/syntelecom/mcSimFlow")
public class McSimFlowController {

    @Autowired
    IMcSimFlowService simFlowService;
    @Autowired
    McSimDxMapper simDxMapper;

    @Autowired
    McSimFlowMapper flowMapper;

    //http://localhost:9000/test/monthFlow?iccid=8986032245200362737&month=202405
    @RequestMapping(value = "/monthFlow")
    @ResponseBody
    public void monthFlow() {
        String month = DateUtils.dateToStr(DateUtils.addMonth(new Date(), -1)).substring(0, 6);

        LambdaQueryWrapper<McSimDx> wrapper = new LambdaQueryWrapper<>();
        wrapper.isNotNull(McSimDx::getBatchNo)
                .ne(McSimDx::getBatchNo, "")
                .and(it -> it.isNull(McSimDx::getImZhuPackage)
                        .or()
                        .notLike(McSimDx::getImZhuPackage, "NB"));

        List<McSimDx> mcSimDxList = simDxMapper.selectList(wrapper);
        Date date = new Date();
        for (McSimDx mcSimDx : mcSimDxList) {
            System.out.println(mcSimDx);
            McSimFlow mcSimFlow=new McSimFlow();
            mcSimFlow.setIccid(mcSimDx.getIccid());
            double flow = simFlowService.monthFlow(mcSimDx.getIccid(), month);
            mcSimFlow.setUsed(Double.toString(flow));
            mcSimFlow.setStartDate(DateUtils.getFirstDayOfMonth(month));
            mcSimFlow.setEndDate(DateUtils.getLastDayOfMonth(month));
            mcSimFlow.setCreateTime(date);
            mcSimFlow.setUpdateTime(date);
            flowMapper.insert(mcSimFlow);
        }


    }

}
